﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameBase
{
    public abstract class VisibleGameEntity : GameEntity
    {
        protected VisibleGroup parent;

        public VisibleGroup Parent
        {
            get { return parent; }
            set { parent = value; }
        }
        protected Vector2 position = Vector2.Zero;
        protected Vector2 basePosition = Vector2.Zero;
        
        protected float scale = 1;
        protected float baseScale = 1;
        
        protected float rotate = 0;
        protected float baseRotate = 0;
        protected float depth = 0.9f;

        public virtual float Depth
        {
            get { return depth; }
            set { depth = value; }
        }
        
        protected bool isVisible = true;

        public virtual Vector2 BasePosition
        {
            get { return basePosition; }
            set { 
                basePosition = value;
            }
        }
        public virtual float BaseScale
        {
            get { return baseScale; }
            set {
                baseScale = value;
            }
        }
        public virtual float BaseRotate
        {
            get { return baseRotate; }
            set { 
                baseRotate = value;
            }
        }

        public virtual Vector2 Position
        {
            get { return position; }
            set { 
                position = value;
            }
        }

        public virtual float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        public virtual float Rotate
        {
            get { return rotate; }
            set { rotate = value; }
        }

        public virtual bool Visible
        {
            get { return isVisible; }
            set { isVisible = value; }
        }

        virtual public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
        }

        public virtual void Translate(float x, float y)
        {
            Position = position + new Vector2(x, y);
        }
        public virtual Vector2 GetDrawPosition()
        {
            float x = position.X*baseScale;
            float y = position.Y*baseScale;
            Vector2 drawPosition = new Vector2((float)(Math.Cos(baseRotate) * x - Math.Sin(baseRotate) * y) + basePosition.X,
                (float)(Math.Sin(baseRotate) * x + Math.Cos(baseRotate) * y) + basePosition.Y);
            return drawPosition;
        }
        public virtual float GetDrawScale()
        {
            return scale*baseScale;
        }
        public float GetDrawRotate()
        {
            return rotate+baseRotate;
        }
       /* public virtual Rectangle GetBound()
        {
            return new Rectangle();
        }*/
        public bool IsHover(float x, float y, Rectangle bound)
        {
            return (x >= bound.X && x <= bound.X + bound.Width
                && y >= bound.Y && y <= bound.Y + bound.Height);
        }
        public Vector2 GetLocalPosition(Vector2 globalPosition)
        {
            Vector2 drawPosition = GetDrawPosition();
            float x = globalPosition.X - drawPosition.X;
            float y = globalPosition.Y - drawPosition.Y;
            x = x / GetDrawScale();
            y = y / GetDrawScale();
            float r = -GetDrawRotate();
            Vector2 localPosition = new Vector2((float)(Math.Cos(r) * x - Math.Sin(r) * y),
                (float)(Math.Sin(r) * x + Math.Cos(r) * y));
            return localPosition;
        }
    }
}
